﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities.Dispatcher;
using Common.Utilities;
using Workflows.Components.TextMining.Properties;

namespace Workflows.Components.TextMining.EnumerationObjects
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class PdfDocListComponent:WorkflowComponentBase
    {
        #region props
        private PdfDocListInstruction _Instruction;
        /// <summary></summary>
        public override InstructionBase Instruction
        {
            get
            {
                return _Instruction;
            }
            set
            {
                _Instruction = (PdfDocListInstruction)value;
            }
        }

        private PdfDocListOutput _Output;
        /// <summary></summary>
        public override ResultBase Output
        {
            get
            {
                return _Output;
            }
            set
            {
                _Output = (PdfDocListOutput)value;
            }
        }

        /// <summary></summary>
        public string ConnStr
        {
            get
            {
                return this._Instruction.ConnStr;
            }
            set
            {
                this._Instruction.ConnStr = value;
            }
        }

        /// <summary></summary>
        public string SelectSqlCommand
        {
            get
            {
                return this._Instruction.SelectSqlCommand;
            }
            set
            {
                this._Instruction.SelectSqlCommand = value;
            }
        }

        /// <summary></summary>
        public string DocIDField
        {
            get
            {
                return this._Instruction.DocIDField;
            }
            set
            {
                this._Instruction.DocIDField = value;
            }
        }

        /// <summary></summary>
        public string BFileFieldName
        {
            get
            {
                return this._Instruction.BFileFieldName;
            }
            set
            {
                this._Instruction.BFileFieldName = value;
            }
        }

        /// <summary></summary>
        public string FileNameField
        {
            get
            {
                return this._Instruction.FileNameField;
            }
            set
            {
                this._Instruction.FileNameField = value;
            }
        }

        /// <summary>
        /// default to {0}, 
        /// if {0}.pdf, it will add a .pdf extention 
        /// to the end as file name
        /// </summary>
        public string FileNameFormat
        {
            get
            {
                return this._Instruction.FileNameFormat;
            }
            set
            {
                this._Instruction.FileNameFormat = value;
            }
        }

        /// <summary></summary>
        public string LocalFolderPath
        {
            get
            {
                return this._Instruction.LocalFolderPath;
            }
            set
            {
                this._Instruction.LocalFolderPath = value;
            }
        }

        /// <summary></summary>
        public string FileFilter
        {
            get
            {
                return this._Instruction.FileFilter;
            }
            set
            {
                this._Instruction.FileFilter = value;
            }
        }

        /// <summary></summary>
        public bool IncludeSubfolders
        {
            get
            {
                return this._Instruction.IncludeSubfolders;
            }
            set
            {
                this._Instruction.IncludeSubfolders = value;
            }
        }

        /// <summary></summary>
        public string ResultFilePath
        {
            get
            {
                return this._Instruction.ResultFilePath;
            }
            set
            {
                this._Instruction.ResultFilePath = value;
            }
        }

        /// <summary></summary>
        public PdfDocListInstruction.DocListCompensationMethod Compensation
        {
            get
            {
                return this._Instruction.Compensation;
            }
            set
            {
                this._Instruction.Compensation = value;
            }
        }
        #endregion

        #region ctor
        ///<summary>
        ///</summary>
        public PdfDocListComponent()
            : base()
        {
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.Enumeration.ToString();
            this.ComponentName = "PDF Doc List";
            this.Description = "Provide pdf file list for downstream workers";
            this.ComponentIcon = Resources.adobe;
            this.ComponentImage = Resources.Form;

            this._Instruction =
                new PdfDocListInstruction(
                    this.ComponentID, Guid.Empty,
                    string.Empty, string.Empty,
                    string.Empty, string.Empty, string.Empty,
                    "{0}", string.Empty, "*.pdf", true, string.Empty,
                    PdfDocListInstruction.DocListCompensationMethod.DbToLocalFile);
            this._Output = new PdfDocListOutput(this.ComponentID, Guid.Empty, string.Empty, 0);
        }
        #endregion

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ConnectionString", this.ConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SelectSqlStatement", this.SelectSqlCommand);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "DocIDField", this.DocIDField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "BFileField", this.BFileFieldName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FileNameField", this.FileNameField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FileNameFormat", this.FileNameFormat);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "LocalFolderPath", this.LocalFolderPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FileFilter", this.FileFilter);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "IncludeSubfolder", this.IncludeSubfolders.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ResultFilePath", this.ResultFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "CompensateMethod", ((int)this.Compensation).ToString());
            
            return xNode;
        }

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                PdfDocListComponent component = base.Instantiate(xNode) as PdfDocListComponent;

                component.ConnStr = XmlDataUtil.GetAttributeValue(xNode, "ConnectionString", "");
                component.SelectSqlCommand = XmlDataUtil.GetAttributeValue(xNode, "SelectSqlStatement", "");
                component.DocIDField = XmlDataUtil.GetAttributeValue(xNode, "DocIDField", "");
                component.BFileFieldName = XmlDataUtil.GetAttributeValue(xNode, "BFileField", "");
                component.FileNameField = XmlDataUtil.GetAttributeValue(xNode, "FileNameField", "");
                component.FileNameFormat = XmlDataUtil.GetAttributeValue(xNode, "FileNameFormat", "{0}");
                component.LocalFolderPath = XmlDataUtil.GetAttributeValue(xNode, "LocalFolderPath", "");
                component.FileFilter = XmlDataUtil.GetAttributeValue(xNode, "FileFilter", "*.pdf");
                component.IncludeSubfolders = bool.Parse(XmlDataUtil.GetAttributeValue(xNode, "IncludeSubfolder", false.ToString()));
                component.ResultFilePath = XmlDataUtil.GetAttributeValue(xNode, "ResultFilePath", "");
                component.Compensation =
                    (PdfDocListInstruction.DocListCompensationMethod)
                    int.Parse(XmlDataUtil.GetAttributeValue(
                                  xNode, "CompensateMethod",
                                  ((int) PdfDocListInstruction.DocListCompensationMethod.DbToLocalFile).ToString()));
                
                return component;
            }
            else
            {
                throw new Exception("Unable to instantiate email: invalid type specified in xml node");
            }
        }
    }
}
